// pages/testReport/testReport.js
const app = getApp();
let tool = require('../../utils/tool.js');
const httpUtils = require('../../utils/httpUtils.js');
import Dialog from '../../dist/dialog/dialog';

//今日复习的界面
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    okNums: 0,
    errNums: 0,
    selNums: {},
    ansNums: {},
    reviewNum: 0, //今日有多少词需复习
    flag: true, //显示隐藏提示复习的弹窗的标志
    innerAudioContext: new Object(),
    allWord: '',
    errWord: '',
    canClick: 0,
    questData: [],
    wordID: 0,
    wordLen: 0,
    readTimes: {},
    page: 1,
    pageSize: 20,
    hasMoreData: false,
    isFirst:true,
    reviewTime:"19:00", //复习推送时间
    reviewStatus: "1", //复习状态 0 复习完成可查看错词；1有复习单词；2 今日无复习任务；3未到复习时间
    task_id:"",
    sort:"15",
    can_submit:"1",
    date:"",
  },
  addErrWord: function(errData) {
    let temWrod = this.data.errWord;
    if (temWrod === "") {
      temWrod = errData;
    } else {
      temWrod = temWrod + "," + errData;
    }
    this.setData({
      errWord: temWrod
    })
  },
  clickErr: function(e) {
    let temWrod = e.currentTarget.dataset.words;
    let temA = this.data.ansNums;
    let errNums = this.data.errNums;
    let okNums = this.data.okNums;
    if (temA[temWrod] == 2) {
      okNums--;
    }
    if (temA[temWrod] != 1){
      errNums++;
    }
    temA[temWrod] = 1;
    this.setData({
      ansNums: temA,
      errNums: errNums,
      okNums: okNums
    })
  },
  clickOk: function(e) {
    console.log("-----------------",JSON.stringify(e));
    let temWrod = e.currentTarget.dataset.words;
    let errNums = this.data.errNums;
    let okNums = this.data.okNums;
    let temA = this.data.ansNums;
    if (temA[temWrod] == 1) {
      errNums--;
    }
    // okNums++;

    if (temA[temWrod] != 2){
      okNums++;
    }
   
    temA[temWrod] = 2;
    this.setData({
      ansNums: temA,
      errNums: errNums,
      okNums: okNums
    })
  },

  clickTxt: function(e) {
    this.data.innerAudioContext.stop();
    let temWrod = e.currentTarget.dataset.words;
    let temNum = this.data.selNums;

    if (temNum[temWrod] === undefined) {
      temNum[temWrod] = 1;
      this.setData({
        selNums: temNum,
        word1Cls: 'xhjc_text3_2'
      });
    } else {
      let rTimes = this.data.readTimes;
      rTimes[temWrod] = 2;
      if (temNum[temWrod] == 2) {
        temNum[temWrod] = 1;
      } else {
        temNum[temWrod] = 2;
      }
      this.setData({
        selNums: temNum,
        readTimes: rTimes
      });

    }
    let audios = e.currentTarget.dataset.audios;
    this.data.innerAudioContext.src = audios;
    this.data.innerAudioContext.play();

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    //获取传过来需复习的单词数
    that.setData({
      // reviewNum: options.reviewNum,
      // reviewStatus: options.reviewStatus,
      // reviewTime: options.push_time,
      task_id:options.task_id,
      sort:options.sort,
      can_submit:options.can_submit,
      date:options.date,
    })
    //获取今日复习数据(学后检测)
    this.getTodayReviewWord();
  },


  /**
   * 获取今日复习的单词 --- 重新调新的接口
   */
  getTodayReviewWord: function() {
    var that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getReviewCheckPass({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      calendar_id:that.data.task_id,
      page: that.data.page,
      size: that.data.pageSize
    }).then((res) => {
      console.log("今日复习界面收到的复习状态：", that.data.reviewStatus)
      if( that.data.isFirst ){
        that.setData({
          reviewNum: res.data.length,
          // flag: false,
          isFirst:false,
        })
      }
      console.log("reviewNum:", that.data.reviewNum)
      var questData = that.data.questData;
      var list = res.data;
      if (that.data.page == 1) {
        questData = []
      }
      console.log("listde length:",list.length)

      if (list.length < that.data.pageSize) {
        that.setData({
          questData: questData.concat(list),
          hasMoreData: false
        })
      } else {
        that.setData({
          questData: questData.concat(list),
          hasMoreData: true,
          page: that.data.page + 1
        })
      }
      console.log("request请求方法中hasmoredata",that.data.hasMoreData)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    console.log("onReachBottom触发了")
    console.log("当前页:", that.data.page)

    if (that.data.hasMoreData) {
      console.log("获取更多数据")
      that.getTodayReviewWord();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 点击提交按钮跳转今日复习报告界面;
   */
  gotourl: function() {
    var that = this;
    if (this.data.errNums + this.data.okNums <=0){
      wx.showModal({
        title: '温馨提示',
        content: "请先复习单词",
        showCancel: false,
      });
      return;
    }

    if(this.data.errNums + this.data.okNums < this.data.reviewNum){
      Dialog.confirm({
        title: '提示',
        message: '还未全部复习完，是否确认提交'
      }).then(() => {
        // on confirm
        that.submitReview();
        
      }).catch(() => {
        // on cancel
        console.log("点击了弹窗的取消按钮")
      });
    }else{
      that.submitReview();
    }

  },

//提交复习
  submitReview: function(){
    let that =this;
    let temD = this.data.ansNums;
    let temA = '';
    let temE = '';
    for (var a in temD) {
      if (temD[a] == 1) {
        if (temE == '') {
          temE = that.data.questData[a].word
        } else {
          temE = temE + "," + that.data.questData[a].word
        }
      }
      if (temD[a] == 1 || temD[a] == 2) {
        if (temA == '') {
          temA = that.data.questData[a].word
        } else {
          temA = temA + "," + that.data.questData[a].word
        }
      }

    }

    wx.showLoading({
      title: '正在提交',
      mask: true
    })
    httpUtils.submitWordReview({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      calendar_id: that.data.task_id,
      sort: that.data.sort,
      pic: "",
      stu_answer: "",
      errword: temE,
      allword: temA,
    }).then((res) => {
      wx.hideLoading();
      wx.redirectTo({
        url: '/pages/reviewRecord/playReport/playReport?task_id=' + that.data.task_id + "&sort=" + that.data.sort + "&can_submit=" + that.data.can_submit + "&date=" + that.data.date + "&type=0",
      })
    });
  },


  //点击新加提示弹窗的开始复习按钮
  beginReview: function() {
    this.setData({
      flag: true
    });
  },

  // 关闭弹窗
  closeMask: function() {
    this.setData({
      flag: true
    });
    wx.navigateBack({
      delta: -1
    });
  }

})